package com.general.framework.core.lang;

import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Objects;

/**
 * @author leejean
 * @ClassName: Numbers
 * @Description: 数字工具类
 * @date 2020年07月01 18:59:29
 */
public abstract class Numbers extends NumberUtils {

    /**
     * 数字常量
     */
    public static final Integer INTEGER_THREE = Integer.valueOf(3);
    public static final Integer INTEGER_FOUR = Integer.valueOf(4);
    public static final Integer INTEGER_FIVE = Integer.valueOf(5);
    public static final Integer INTEGER_SIX = Integer.valueOf(6);
    public static final Integer INTEGER_SEVEN = Integer.valueOf(7);
    public static final Integer INTEGER_EIGHT = Integer.valueOf(8);
    public static final Integer INTEGER_NINE = Integer.valueOf(9);


    /**
     * 格化数字
     *
     * @param number
     * @param formatter
     * @return
     */
    public static String format(final Number number, final String formatter) {
        DecimalFormat df = new DecimalFormat(formatter);
        return df.format(number);
    }

    /**
     * Long 转换成 Integer
     *
     * @param number
     * @return
     */
    public static Long intToLong(final Integer number) {
        if (number == null) {
            return null;
        }
        return number.longValue();
    }

    /**
     * 两个数字相除
     *
     * @param num1
     * @param num2
     * @param scale
     * @return
     */
    public static BigDecimal divide(final Long num1, final Long num2, final Integer scale) {
        if (num1 == null || num2 == null) {
            return BigDecimal.ZERO;
        }
        if (Objects.equals(Numbers.LONG_ZERO, num2)) {
            return BigDecimal.ZERO;
        }
        final BigDecimal number1 = BigDecimal.valueOf(num1);
        final BigDecimal number2 = BigDecimal.valueOf(num2);
        return number1.divide(number2, scale, BigDecimal.ROUND_HALF_UP);
    }


}
